﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Text;
using System.Reflection;
using Microsoft.Phone.Tasks;
using System.Diagnostics;
using System.IO.IsolatedStorage;

namespace PCHenry.miFlashlight
{
	public partial class MainPage : PhoneApplicationPage
	{
		static IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;

		double brightness = 0.0;
		double maxDimmer = 0.9;
		double startingOpacity = 0.0;
		double startingY = 0.0;
		double originalDimmerIntensity = 0.0;

		#region ctor
		// Constructor
		public MainPage()
		{
			InitializeComponent();

			//UpdateOrAdd( "FlashlightColor", Colors.Yellow.ToString() );

			GetFlashlightSettings();
			originalDimmerIntensity = Dimmer.Opacity;
		}

		#endregion

		#region Events

		//private void CloseButton_Click( object sender, RoutedEventArgs e )
		//{
		//  //NavigationService.GoBack();
		//  throw new ApplicationNeedsToExitException();
		//}

		private void ApplicationBarAbout_Click( object sender, EventArgs e )
		{
			NavigationService.Navigate( new Uri( "/About.xaml", UriKind.Relative ) );
		}

		private void ApplicationBarHelp_Click( object sender, EventArgs e )
		{
			NavigationService.Navigate( new Uri( "/Help.xaml", UriKind.Relative ) );
		}

		private void ApplicationBarVisitMiFlashlight_Click( object sender, EventArgs e )
		{
			WebBrowserTask browser = new WebBrowserTask();
			browser.URL = "http://miFlashlight.PCHenry.com";
			browser.Show();
		}

		private void ApplicationBarSettings_Click( object sender, EventArgs e )
		{
			NavigationService.Navigate( new Uri( "/Settings.xaml", UriKind.Relative ) );
		}

		private void Brightness_ManipulationCompleted( object sender, ManipulationCompletedEventArgs e )
		{
			///todo write to the ApplicationSettings
		}

		private void Brightness_ManipulationStarted( object sender, ManipulationStartedEventArgs e )
		{
			startingY = e.ManipulationOrigin.Y;
			startingOpacity = Dimmer.Opacity;
		}

		private void Brightness_ManipulationDelta( object sender, ManipulationDeltaEventArgs e )
		{
			double deltaY = e.ManipulationOrigin.Y;
			double deltaPercetage = (deltaY - startingY) / Dimmer.Height;
			double newOpacity = 1 - ((1 - startingOpacity) * (1 - deltaPercetage));

			//make sure it's within acceptable range
			if( newOpacity < 0 )
			{
				newOpacity = 0;
			}
			else if( newOpacity > maxDimmer )
			{
				newOpacity = maxDimmer;
			}

			Dimmer.Opacity = newOpacity;
		}

		private void FlashlightDimmer_GestureListenerDoubleTap( object sender, GestureEventArgs e )
		{
			double newOpacity = 0.0;

			if( Dimmer.Opacity == 0.0 )
			{
				newOpacity = .9;
			}

			Dimmer.Opacity = newOpacity;
		}

		private void ApplicationBar_StateChanged( object sender, Microsoft.Phone.Shell.ApplicationBarStateChangedEventArgs e )
		{
			if( e.IsMenuVisible )
			{
				originalDimmerIntensity = Dimmer.Opacity;
				Dimmer.Opacity = maxDimmer;
			}
			else
			{
				Dimmer.Opacity = originalDimmerIntensity;
			}
		}

		private void ApplicationBarIconButton_Click( object sender, EventArgs e )
		{

		}

		private void PhoneApplicationPage_Loaded( object sender, RoutedEventArgs e )
		{
			//ContentPanel.Background = new SolidColorBrush( flashlightColor );
		}

		#endregion

		#region Methods
		private void GetFlashlightSettings()
		{
			IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;

			//if( settings.Contains( "FlashlightColor" ) )
			//{
			//  settings.TryGetValue<Color>( "FlashlightColor", out flashlightColor );
			//}

			//if( flashlightColor == null )
			//{
			//  FlashlightColor = Colors.White;
			//}

			//ContentPanel.Background = new SolidColorBrush( flashlightColor );
			Dimmer.Opacity = brightness;
		}

		//public static void UpdateOrAdd( string key, object value )
		//{
		//  if( settings.Contains( key ) )
		//  {
		//    settings[key] = value;
		//  }
		//  else
		//  {
		//    settings.Add( key, value );
		//  }

		//  settings.Save();
		//}

		#endregion
	}
}